import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { Encrypt } from '@/utils/enAes.js'

Vue.config.productionTip = false

// echarts
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts

// vant
import Vant from 'vant'
import 'vant/lib/index.css'
Vue.use(Vant)

// 全局配置
import api from './configs/apiConfig'
import config from './configs/config'
import http from './utils/httpUtils'
import utils from './utils/utils'
import { showItem, hideItem } from '@/utils/appUtils'
// import dateUtils from './utils/dateUtils';

Vue.prototype.showItem = showItem
Vue.prototype.hideItem = hideItem
// Vue.prototype.getToken = getToken

Vue.prototype.$A = api
Vue.prototype.$C = config
// Vue.prototype.$D = dateUtils;
Vue.prototype.$H = http
Vue.prototype.$U = utils

router.beforeEach(async (to, from, next) => {
    // getToken()
    let u = navigator.userAgent
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
    let isIos = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
    if (isAndroid) {
        store.commit('setToken', jsBridge.getToken())
        return next()
    }
    if (isIos) {
        window['getToken'] = (res) => {
            let resDate = res.split(',')
            store.commit('setToken', resDate[0])
            store.commit('setIosType', resDate[1])
            return next()
        }
    }
    if (to.query.userName && to.query.password) {
        const loginData = {
            userAcc: to.query.userName,
            userPassword: Encrypt(to.query.password),
            corpNo: config.corpNo
        }
        const res = await http.login(api.login, loginData)
        store.commit('setToken', res)
        next()
    }
    return next()
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
